package com.qdone.module.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qdone.common.util.GenUtils;
import com.qdone.framework.core.page.CoreUtil;
import com.qdone.framework.core.page.PageInfo;
import com.qdone.framework.core.page.PageList;
import com.qdone.module.dao.SysCodeDao;
import com.qdone.module.model.TableData;
import freemarker.template.Configuration;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月19日 下午3:33:38
 */
@Service
public class SysCodeService {

	@Autowired
	private SysCodeDao sysCodeDao;

	@Autowired
	Configuration freemaker;


	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public PageList<TableData> selectPage(TableData object) {
		PageList<TableData> pageList = new PageList<TableData>();
		PageInfo pageInfo = CoreUtil.createBootStrapPage(object, true);
		String orders = CoreUtil.createSort(pageInfo, true);
		/*PageHelper.offsetPage(pageInfo.getCurrentNumber(), pageInfo.getPageSize());
		PageHelper.orderBy(orders);// 插件排序*/
		PageHelper.startPage(pageInfo.getCurrentNumber(), pageInfo.getPageSize(),orders);
		List<TableData> list = sysCodeDao.select(object);
		pageList.setList(list);
		Page<TableData> pg = (Page<TableData>) list;
		pageInfo.setRecordCount(pg.getTotal());// 设置总记录数
		pageList.setPageInfo(pageInfo);
		return pageList;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public  PageList<Map<String,Object>> queryPage(Map<String, Object> map) {
		PageList<Map<String,Object>> pageList = new PageList<Map<String,Object>>();
		PageInfo pageInfo = CoreUtil.buildBootStrapPage(map, false);
		String orders = CoreUtil.createSort(pageInfo, true);
		PageHelper.offsetPage(pageInfo.getCurrentNumber(), pageInfo.getPageSize());
		PageHelper.orderBy(orders);// 插件排序
		List<Map<String,Object>> list = sysCodeDao.queryList(map);
		pageList.setList(list);
		Page<Map<String,Object>> pg = (Page<Map<String,Object>>) list;
		pageInfo.setRecordCount(pg.getTotal());// 设置总记录数
		pageList.setPageInfo(pageInfo);
		return pageList;
	}

	public List<Map<String, Object>> queryList(Map<String, Object> map) {
		return sysCodeDao.queryList(map);
	}

	public int queryTotal(Map<String, Object> map) {
		return sysCodeDao.queryTotal(map);
	}

	public Map<String, String> queryTable(String tableName) {
		return sysCodeDao.queryTable(tableName);
	}

	public List<Map<String, String>> queryColumns(String tableName) {
		return sysCodeDao.queryColumns(tableName);
	}

	public byte[] generatorCode(String[] tableNames) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);

		for(String tableName : tableNames){
			//查询表信息
			Map<String, String> table = queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			//生成代码
			GenUtils.generatorCode(freemaker,table, columns, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}
}
